package poo.comparable;

import java.util.GregorianCalendar;

public class Data implements Comparable<Data> {

	private final int G, M, A;

	public enum Specie {
		GIORNO, MESE, ANNO
	};

	public Data() {
		GregorianCalendar gc = new GregorianCalendar();
		G = gc.get(GregorianCalendar.DAY_OF_MONTH);
		M = gc.get(GregorianCalendar.MONTH) + 1;
		A = gc.get(GregorianCalendar.YEAR);
	}// Data

	public Data(int g, int m, int a) {
		if (a < 0 || m < 1 || m > 12 || g < 1 || g > durataMese(m, a))
			throw new IllegalArgumentException();
		this.G = g;
		this.M = m;
		this.A = a;
	}// Data

	public Data(Data d) {
		G = d.G;
		M = d.M;
		A = d.A;
	}// Data

	public int get(Specie cosa) {
		switch (cosa) {
		case GIORNO:
			return G;
		case MESE:
			return M;
		default:
			return A;
		}
	}// get

	public static int durataMese(int m, int a) {
		if (m < 1 || m > 12 || a < 0)
			throw new IllegalArgumentException();
		int durata;
		switch (m) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			durata = 31;
			break;
		case 2:
			durata = bisestile(a) ? 29 : 28;
			break;
		default:
			durata = 30;
		}// switch
		return durata;
	}// durataMese

	public static boolean bisestile(int a) {
		if (a < 0)
			throw new IllegalArgumentException();
		if (a % 4 != 0)
			return false;
		if (a % 100 == 0 && a % 400 != 0)
			return false;
		return true;
	}// bisestile

	public String toString() {
		return G + "/" + M + "/" + A;
	}// toString

	public int compareTo(Data d) {// si evita un operazione di casting
		if (this.equals(d))
			return 0;
		if (this.A < d.A || this.A == d.A && this.M < d.M || this.A == d.A
				&& this.M == d.M && this.G < d.G)
			return -1;
		return +1;

	}

}
